﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// photo_list 的摘要说明
    /// </summary>
    public class photo_list : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount = 0;
        protected int page = 1;
        protected int pageSize = 0;
        public void ProcessRequest(HttpContext context)
        {
            this.pageSize = 15;
            this.page = DTRequest.GetQueryInt("page", 1);
            string keywords = DTRequest.GetQueryString("keywords");
            string caizhi_id = DTRequest.GetQueryString("caizhi_id");
            string wenli_id = DTRequest.GetQueryString("wenli_id");
            int yongtu_id = DTRequest.GetQueryInt("yongtu_id", 0);
            string yanse_id = DTRequest.GetQueryString("yanse_id");
            string txtProvnce = DTRequest.GetQueryString("txtProvnce");
            int txtCity = DTRequest.GetQueryInt("txtCity", 0);
            int txtDistrict = DTRequest.GetQueryInt("txtDistrict", 0);


            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取供应信息
            BLL.photos bll = new BLL.photos();
            string strWhere = string.Empty;
            strWhere = " 1=1 ";
            if (keywords + "" != "") {
                strWhere += " and title like '%" + keywords + "%'";
            }
            if (caizhi_id +""!="")
            {
                strWhere += " and caizhi_id in( select id from ch_caizhi where title  like '%" + caizhi_id+"%')";
            }
            if (wenli_id + "" != "")
            {
                strWhere += " and wenli_id in( select id from ch_wenli where title  like '%" + wenli_id + "%')";
            }
            if (yongtu_id > 0)
            {
                strWhere += " and yongtu_id=" + yongtu_id;
            }
            if (yanse_id + "" != "")
            {
                strWhere += " and yanse_id in( select id from ch_yanse where title  like '%" + yanse_id + "%')";
            }

            if (txtProvnce + "" != "")
            {
                strWhere += " and province_id in( select id from ch_province where province_name  like '%" + txtProvnce + "%')";
            }
            if (txtCity > 0)
            {
                strWhere += " and city_id=" + txtCity;
            }
            if (txtDistrict > 0)
            {
                strWhere += " and district_id =" + txtDistrict;
            }
            //Utils.WriteTextLog("photo_list", "keywords:" + keywords + ",pwd:" + strWhere, DateTime.Now);
            DataSet ds = bll.GetList(this.pageSize, this.page, strWhere, "id desc", out this.totalCount);
            decimal t_page = Math.Ceiling(decimal.Parse((this.totalCount / this.pageSize).ToString()));
            int total_page =int.Parse(t_page.ToString());
            strTxt.Append("\"total_page\":" + total_page.ToString() + ",");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"photo_list\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr3 = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr3["id"]);
                    strTxt.Append(",\"title\":\"" + dr3["title"] + "\"");
                    if (string.IsNullOrEmpty(dr3["img_url"].ToString()))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else
                    {
                        string img_url = dr3["img_url"].ToString();
                        //strTxt.Append(",\"img_url\":\"http:\\/\\/" + dr3["img_url"] + "\"");

                        if (img_url.Contains("shicaiquanzi"))
                        {
                            if (img_url.Contains("http://")) { strTxt.Append(",\"img_url\":\"" + img_url + "\""); }
                            else { strTxt.Append(",\"img_url\":\"http://" + img_url + "\""); }

                        }
                        else
                        {
                            strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + img_url + "\"");
                        }
                    }
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                strTxt.Append("\"status\":2,\"photo_list\":[]");
            }
            strTxt.Append("}");

            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}